#pragma once
#include "Device_Motor.h"
#include "Device_Motor_FuYu.h"
#include <functional>
#include <map>
#include <memory>
#include "Device_Motor_Default.h"

class Device_Motor_Factory {
public:
    using CreatorFunc = std::function<std::shared_ptr<Device_Motor>(QObject*)>;

    static Device_Motor_Factory& instance() {
        static Device_Motor_Factory instance;
        return instance;
    }

    void registerType(MotorType type, CreatorFunc creator) {
        creators[type] = std::move(creator);
    }

    std::shared_ptr<Device_Motor> createMotor(MotorType type, QObject* parent = nullptr) {
        auto it = creators.find(type);
        if (it != creators.end()) {
            return it->second(parent);
        }
        return nullptr;
    }

    std::shared_ptr<Device_Motor> createMotor(QObject* parent = nullptr) {
        return std::make_shared<Device_Motor_Default>(parent);
    }

    template<typename T>
    void registerDerivedType(MotorType type) {
        registerType(type, [](QObject* parent) {
            return std::make_shared<T>(parent);
        });
    }

private:
    Device_Motor_Factory() {
        registerDerivedType<Device_Motor_Default>(MotorType::Device_Motor_Default);
        registerDerivedType<Device_Motor_FuYu>(MotorType::Device_Motor_FuYu);


    }

    std::map<MotorType, CreatorFunc> creators;
};
